package service

import (
	"ruoyi-go/internal/core/constants"
	"ruoyi-go/internal/web/system/domain"
)

// GetRolePermissions 查询用户的角色列表
func GetRolePermissions(user *domain.SysUser) []string {
	if user.IsAdmin() {
		return []string{"admin"} // 返回超管权限
	}
	return SelectRolePermissionByUserId(user.UserID)
}

// GetMenuPermissions 查询菜单权限
func GetMenuPermissions(user *domain.SysUser) []string {
	if user.IsAdmin() {
		return []string{"*:*:*"} // 返回超管权限
	}
	perms := make([]string, 0)
	roles := user.Roles
	if len(roles) > 0 { // 多角色设置permissions属性，以便数据权限匹配权限
		for _, role := range roles {
			if !role.IsAdmin() && role.Status == constants.ROLE_NORMAL {
				rolePerms := SelectMenuPermsByRoleId(role.RoleId)
				if len(rolePerms) > 0 {
					perms = append(perms, rolePerms...)
				}
			}
		}
	} else {
		userPerms := SelectMenuPermsByUserId(user.UserID)
		if len(userPerms) > 0 {
			perms = append(perms, userPerms...)
		}
	}
	return perms
}
